<?php
//
// +------------------------------------------------------------------------+
// | PHP Version 5                                                          |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | This source file is subject to version 3.00 of the PHP License,        |
// | that is available at http://www.php.net/license/3_0.txt.               |
// | If you did not receive a copy of the PHP license and are unable to     |
// | obtain it through the world-wide-web, please send a note to            |
// | license@php.net so we can mail you a copy immediately.                 |
// +------------------------------------------------------------------------+
// | Author:                                                                |
// +------------------------------------------------------------------------+
//
// $Id$
//


/**
 * @author       mk
 */
abstract class BaseAction {
    
    /**
     * @var      PhpView
     */
    protected $view;
    protected $params = array(),
              $viewVars = array(),
              $layout,
              $template_dir;
            
    /**
     * @return   void
     */
    public function __construct() { }
    
    public function execute() {
        $action = camelize($this->params['action']);
        $action = 'action'.$action;
        if (!method_exists($this, $action)) {
            Request::getInstance()->render404();
        }
        call_user_func(array($this, $action));
    }
    
    /**
     * @return   void
     */
    public function render($template = null, $m=null) {
        $this->view->render($this->getTemplatePath($template, $m));
    }
    
    public function setParams($params) {
        $this->params = $params;
    }
    
    public function postVar($name) {
        return isset($_POST[$name])?$_POST[$name]:null;
    }
    
    public function getVar($name) {
        return isset($_GET[$name])?$_GET[$name]:null;
    }
    
    /**
     * @return   PhpView
     */
    public function getView() {
        return $this->view;
    }
    
    /**
     * @param    PhpView $view    
     * @return   void
     */
    public function setView(PhpView &$view) {
        $this->view = $view;
        if (!empty($this->layout)) {
            $this->view->layout = $this->layout;
        }
    }
    
    /**
     * 获取模板文件路径
     *
     * @param string $template
     * @return string
     */
    public function getTemplatePath($template = null,$m=null) {
        if(null == $m){
            $m = underscore($this->params['module']);
        }
        $a = underscore($this->params['action']);
        $t = ROOT.'/modules/'.$m.'/template/';
        
        if($this->template_dir){
            $t  = str_replace($m, 'user', $t);
            //echo $t;
            $t  = $t . $this->template_dir.'/';
            //$t  = str_replace($m, $this->template_dir, $t);
        }
        return null === $template ? $t . $a . '.php' : $t . $template . '.php';
    }
    
    /**
     * 设置视图 title
     *
     * @param string $title
     */
    public function setPageTitle($title) {
        $this->view->pageTitle = $title;
    }
    
    /**
     * 模板目录
     * @param type $dir 
     */
    public function setTemplateDir($dir){
        $this->template_dir = $dir;
    }
    
    /**
     * 为模板变量赋值
     *
     * @param string $var
     * @param mixed $val
     */
    public function set($var, $val = null) {
        $this->view->set($var, $val);
    }
    
    /**
     * 可以使用 $this->var 形式为模板赋值
     *
     * @param string $name
     * @param mixed $value
     */
    public function __set($name, $value) {
        if (!in_array($name, array_keys(get_object_vars($this)))) {
            $this->viewVars[$name] = $value;
            $this->view->set($name, $value);
        } else {
            $this->$name = $value;
        }
    }
    
    /**
     * 取回为模板赋的植
     *
     * @param string $name
     * @return mixed
     */
    public function __get($name) {
        if (!in_array($name, array_keys(get_object_vars($this)))) {
            return $this->viewVars[$name];
        } else {
            return $this->$name;
        }
    }
    
    /**
     * @param    string $url    
     * @param    int $statusCode    
     * @return   void
     */
    public function redirect($url, $statusCode = 302, $exit = true) {
        $codes = array(
            100 => "Continue",
            101 => "Switching Protocols",
            200 => "OK",
            201 => "Created",
            202 => "Accepted",
            203 => "Non-Authoritative Information",
            204 => "No Content",
            205 => "Reset Content",
            206 => "Partial Content",
            300 => "Multiple Choices",
            301 => "Moved Permanently",
            302 => "Found",
            303 => "See Other",
            304 => "Not Modified",
            305 => "Use Proxy",
            307 => "Temporary Redirect",
            400 => "Bad Request",
            401 => "Unauthorized",
            402 => "Payment Required",
            403 => "Forbidden",
            404 => "Not Found",
            405 => "Method Not Allowed",
            406 => "Not Acceptable",
            407 => "Proxy Authentication Required",
            408 => "Request Time-out",
            409 => "Conflict",
            410 => "Gone",
            411 => "Length Required",
            412 => "Precondition Failed",
            413 => "Request Entity Too Large",
            414 => "Request-URI Too Large",
            415 => "Unsupported Media Type",
            416 => "Requested range not satisfiable",
            417 => "Expectation Failed",
            500 => "Internal Server Error",
            501 => "Not Implemented",
            502 => "Bad Gateway",
            503 => "Service Unavailable",
            504 => "Gateway Time-out"
        );
        $msg = $codes[$statusCode];
        $status = "HTTP/1.1 {$statusCode} {$msg}";
        header($status);
        header('Location: ' . Router::url($url, true));
        if ($exit) {
            exit();
        }
    }
    
    /**
     * @param    string $url    
     * @param    int $statusCode    
     * @return   void
     */
    public function redirectWeb($url, $statusCode = 302, $exit = true) {
        $codes = array(
            100 => "Continue",
            101 => "Switching Protocols",
            200 => "OK",
            201 => "Created",
            202 => "Accepted",
            203 => "Non-Authoritative Information",
            204 => "No Content",
            205 => "Reset Content",
            206 => "Partial Content",
            300 => "Multiple Choices",
            301 => "Moved Permanently",
            302 => "Found",
            303 => "See Other",
            304 => "Not Modified",
            305 => "Use Proxy",
            307 => "Temporary Redirect",
            400 => "Bad Request",
            401 => "Unauthorized",
            402 => "Payment Required",
            403 => "Forbidden",
            404 => "Not Found",
            405 => "Method Not Allowed",
            406 => "Not Acceptable",
            407 => "Proxy Authentication Required",
            408 => "Request Time-out",
            409 => "Conflict",
            410 => "Gone",
            411 => "Length Required",
            412 => "Precondition Failed",
            413 => "Request Entity Too Large",
            414 => "Request-URI Too Large",
            415 => "Unsupported Media Type",
            416 => "Requested range not satisfiable",
            417 => "Expectation Failed",
            500 => "Internal Server Error",
            501 => "Not Implemented",
            502 => "Bad Gateway",
            503 => "Service Unavailable",
            504 => "Gateway Time-out"
        );
        $msg = $codes[$statusCode];
        $status = "HTTP/1.1 {$statusCode} {$msg}";
        header($status);
        header('Location: ' . $url);
        if ($exit) {
            exit();
        }
    }
}

?>